package agent.timemonitor;

import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;

/**
 * agent.simple.agentmain.MyAgent
 *
 * @author cyril
 * @date 2022/3/8 10:36 下午
 * <p>
 * 日进一寸 日拱一卒
 */
public class MyAgent {

    // 同时存在，优先运行该方法
//    public static void agentmain(String agentArgs, Instrumentation instrumentation) {
//        System.out.println("agent main start 111");
//        System.out.println("args: " + agentArgs);
//    }

    public static void agentmain(String agentArgs) {
        System.out.println("agent main start 222");
        System.out.println("args: " + agentArgs);
    }

    public static void agentmain(String agentArgs, Instrumentation instrumentation) throws UnmodifiableClassException, ClassNotFoundException {
        System.out.println("method-time-monitor-agent agentmain start");
        System.out.println("args: " + agentArgs);
        instrumentation.addTransformer(new MethodTimeMonitorTranformer(), true);

        instrumentation.retransformClasses(Class.forName("appdemo.BusinessService"));
        // java -javaagent:../../method-time-monitor-agent/target/method-time-monitor-agent-1.0-SNAPSHOT.jar -jar app-demo-1.0-SNAPSHOT.jar
    }

    public static void premain(String agentArgs, Instrumentation instrumentation) {
        System.out.println("method-time-monitor-agent premain start");
        System.out.println("args: " + agentArgs);
//        instrumentation.addTransformer(new MethodTimeMonitorTranformer());

        // java -javaagent:../../method-time-monitor-agent/target/method-time-monitor-agent-1.0-SNAPSHOT-jar-with-dependencies.jar -jar app-demo-1.0-SNAPSHOT.jar
    }

}
